package GUI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import Control.ProductCtrl;

public class ProductGUI {

	JPanel panelShow = new JPanel();
	ProductCtrl productCtr = new ProductCtrl();
	
	public ProductGUI() {

	}

	public JPanel add() {

		// set up panel
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Add Customer",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 11, 200, 298);
		panel.setLayout(null);
		// ---------------------------

		//generate textareas, lebels, etc...
		JLabel lName = new JLabel("ID");
		JLabel lAddress = new JLabel("Name");
		JLabel lZip = new JLabel("Purchase Price");
		JLabel lCity = new JLabel("Sales Price");
		JLabel lPhone = new JLabel("Made in");
		JLabel lType = new JLabel("Mini Stock");
		JLabel lId = new JLabel("Amount");
		final JLabel lSuplId = new JLabel("Supl ID");
		JButton addButton = new JButton("Create");

		// set positions
		lName.setBounds(47, 14, 46, 14);
		lAddress.setBounds(47, 40, 46, 14);
		lZip.setBounds(47, 71, 46, 14);
		lCity.setBounds(47, 102, 46, 14);
		lPhone.setBounds(47, 133, 46, 14);
		lType.setBounds(47, 164, 46, 14);
		lId.setBounds(47, 195, 46, 14);
		lSuplId.setBounds(47, 226, 46, 14);
		addButton.setBounds(100, 265, 89, 23);

		// --
		final JTextField nameField = new JTextField();
		final JTextField addressField = new JTextField();
		final JTextField ziptField = new JTextField();
		final JTextField cityField = new JTextField();
		final JTextField phoneField = new JTextField();
		final JTextField typeField = new JTextField();
		JTextField idField = new JTextField();
		final JTextField supplierIdField = new JTextField();

		// set positions
		nameField.setBounds(103, 11, 86, 20);
		addressField.setBounds(103, 37, 86, 20);
		ziptField.setBounds(103, 68, 86, 20);
		cityField.setBounds(103, 99, 86, 20);
		phoneField.setBounds(103, 130, 86, 20);
		typeField.setBounds(103, 161, 86, 20);
		idField.setBounds(103, 192, 86, 20);
		supplierIdField.setBounds(103, 223, 86, 20);

		// ---------------

		// adds to panel
		// text fields
		panel.add(nameField);
		panel.add(addressField);
		panel.add(ziptField);
		panel.add(cityField);
		panel.add(phoneField);
		panel.add(typeField);
		panel.add(idField);
		panel.add(supplierIdField);
		// lebels
		panel.add(lName);
		panel.add(lAddress);
		panel.add(lZip);
		panel.add(lCity);
		panel.add(lPhone);
		panel.add(lType);
		panel.add(lId);
		panel.add(lSuplId);
		panel.add(addButton);
		// ---------

		// sets columns ... not sure why?
		nameField.setColumns(10);
		addressField.setColumns(10);
		ziptField.setColumns(10);
		cityField.setColumns(10);
		phoneField.setColumns(10);
		typeField.setColumns(10);
		idField.setColumns(10);
		// -----------------------

		addButton.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e){
			    System.out.println("doge");
				int productID;
				double salesPrice;
				double pPrice;
				int miniStock;
				int suplyId;
				try{
				    	productID = Integer.valueOf(nameField.getText()).intValue();
					salesPrice = Double.valueOf(cityField.getText()).doubleValue();
					pPrice = Double.valueOf(ziptField.getText()).doubleValue();
					suplyId = Integer.valueOf(supplierIdField.getText()).intValue();
					miniStock = Integer.valueOf(typeField.getText()).intValue();
					screen(productCtr.createProduct(productID, addressField.getText(), pPrice, salesPrice, phoneField.getText(), miniStock, suplyId));
				}
				catch(NumberFormatException er){
				    
				    er.printStackTrace();
				}
				
				
			}
			
			
		});
		return panel;
	}

	public JPanel update() {

		// set up panel
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Update",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(300, 11, 200, 298);
		panel.setLayout(null);
		// ---------------------------

		// Generate textareas, lebels, etc ..
		JLabel lName = new JLabel("ID");
		JLabel lAddress = new JLabel("Name");
		JLabel lZip = new JLabel("Purchase Price");
		JLabel lCity = new JLabel("Sales Price");
		JLabel lPhone = new JLabel("Made in");
		JLabel lType = new JLabel("Mini Stock");
		JLabel lId = new JLabel("Amount");
		JLabel lSuplId = new JLabel("Supl ID");
		JButton updateButton = new JButton("Create");

		// set positions
		lName.setBounds(47, 14, 46, 14);
		lAddress.setBounds(47, 40, 46, 14);
		lZip.setBounds(47, 71, 46, 14);
		lCity.setBounds(47, 102, 46, 14);
		lPhone.setBounds(47, 133, 46, 14);
		lType.setBounds(47, 164, 46, 14);
		lId.setBounds(47, 195, 46, 14);
		lSuplId.setBounds(47, 226, 46, 14);
		updateButton.setBounds(100, 265, 89, 23);

		// --
		final JTextField nameField = new JTextField();
		final JTextField addressField = new JTextField();
		final JTextField ziptField = new JTextField();
		final JTextField cityField = new JTextField();
		final JTextField phoneField = new JTextField();
		final JTextField typeField = new JTextField();
		JTextField idField = new JTextField();
		final JTextField supplierIdField = new JTextField();

		// set positions
		nameField.setBounds(103, 11, 86, 20);
		addressField.setBounds(103, 37, 86, 20);
		ziptField.setBounds(103, 68, 86, 20);
		cityField.setBounds(103, 99, 86, 20);
		phoneField.setBounds(103, 130, 86, 20);
		typeField.setBounds(103, 161, 86, 20);
		idField.setBounds(103, 192, 86, 20);
		supplierIdField.setBounds(103, 223, 86, 20);

		// ---------------

		// adds to panel
		// text fields
		panel.add(nameField);
		panel.add(addressField);
		panel.add(ziptField);
		panel.add(cityField);
		panel.add(phoneField);
		panel.add(typeField);
		panel.add(idField);
		panel.add(supplierIdField);
		// lebels
		panel.add(lName);
		panel.add(lAddress);
		panel.add(lZip);
		panel.add(lCity);
		panel.add(lPhone);
		panel.add(lType);
		panel.add(lId);
		panel.add(lSuplId);
		panel.add(updateButton);
		// ---------

		// sets columns ... not sure why?
		nameField.setColumns(10);
		addressField.setColumns(10);
		ziptField.setColumns(10);
		cityField.setColumns(10);
		phoneField.setColumns(10);
		typeField.setColumns(10);
		idField.setColumns(10);

		
updateButton.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e){
				
			    int productID;
				double salesPrice;
				double pPrice;
				int miniStock;
				int suplyId;
				try{
				    	productID = Integer.valueOf(nameField.getText()).intValue();
					salesPrice = Double.valueOf(cityField.getText()).doubleValue();
					pPrice = Double.valueOf(ziptField.getText()).doubleValue();
					suplyId = Integer.valueOf(supplierIdField.getText()).intValue();
					miniStock = Integer.valueOf(typeField.getText()).intValue();
					screen(productCtr.updateProduct(productID, addressField.getText(), pPrice, salesPrice, phoneField.getText(), miniStock, suplyId));
				}
				catch(NumberFormatException er){
				    
				    er.printStackTrace();
				}
			}
			
			
		});
		return panel;
	}

	public JPanel delete() {

		// set up panel
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Delete",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(540, 11, 208, 71);
		panel.setLayout(null);
		// ---------------------------

		// Generate textareas, lebels, etc ..
		JLabel lName = new JLabel("ID");
		lName.setBounds(31, 14, 46, 14);
		JButton deleteButton = new JButton("Delete");
		deleteButton.setBounds(100, 42, 89, 22);

		final JTextField nameField = new JTextField();
		nameField.setBounds(103, 11, 86, 20);

		// ---------------

		// adds to panel
		// text fields
		panel.add(nameField);
		panel.add(deleteButton);

		// lebels
		panel.add(lName);
		// ---------

		// sets columns ... not sure why?
		nameField.setColumns(10);

		// -----------------------
deleteButton.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e){
				
			    int id;
			    try{
				id = Integer.valueOf(nameField.getText()).intValue();
				screen(productCtr.deleteProduct(id));
			    }catch(NumberFormatException e1){
				
				
			    }
			}
			
			
		});
		return panel;
	}

	public JPanel find() {

		// set up panel
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Find",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(540, 93, 208, 71);
		panel.setLayout(null);
		// ---------------------------

		// Generate textareas, lebels, etc ..
		JLabel lName = new JLabel("ID");
		lName.setBounds(31, 14, 46, 14);
		JButton findButton = new JButton("Find");
		findButton.setBounds(100, 42, 89, 22);
		final JTextField nameField = new JTextField();
		nameField.setBounds(103, 11, 86, 20);

		// ---------------

		// adds to panel
		// text fields
		panel.add(nameField);
		panel.add(findButton);

		// lebels
		panel.add(lName);
		// ---------

		// sets columns ... not sure why?
		nameField.setColumns(10);

		// -----------------------

		findButton.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e){
				
				
				
			    int id;
			    try{
				id = Integer.valueOf(nameField.getText()).intValue();
				screen("Name: " + productCtr.findProduct(id).getName() + " | Price: " + productCtr.findProduct(id).getPurchasePrice() + " | Sales price: " + productCtr.findProduct(id).getSalesPrice());
				
			    }catch(Exception e1){
				
				screen("Not found!!");
			    }
				
			}
			
			
		});
		return panel;
	}

	public JPanel screen(String i) {

		
		// set up panel
		
		panelShow.removeAll();
		panelShow.repaint();
		
		panelShow.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		panelShow.setBounds(10, 311, 738, 210);
		panelShow.setLayout(null);
		
		JLabel text = new JLabel(i);
		text.setBounds(47, 14, 300, 14);
		panelShow.add(text);
		
		
		return panelShow;
	}

}
